<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Models\SCoursewareInfo;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class CoursewareController extends Controller {
	use HasResourceActions;

	/**
	 * Index interface.
	 *
	 * @param Content $content
	 * @return Content
	 */
	public function index(Content $content) {
		return $content
			->header('课件管理')
			->description('列表')
			->body($this->grid());
	}

	/**
	 * Show interface.
	 *
	 * @param mixed $id
	 * @param Content $content
	 * @return Content
	 */
	public function show($id, Content $content) {
		return $content
			->header('课件管理')
			->description('详情')
			->body($this->detail($id));
	}

	/**
	 * Edit interface.
	 *
	 * @param mixed $id
	 * @param Content $content
	 * @return Content
	 */
	public function edit($id, Content $content) {
		return $content
			->header('课件管理')
			->description('编辑')
			->body($this->form()->edit($id));
	}

	/**
	 * Create interface.
	 *
	 * @param Content $content
	 * @return Content
	 */
	public function create(Content $content) {
		return $content
			->header('课件管理')
			->description('新增')
			->body($this->form());
	}

	/**
	 * Make a grid builder.
	 *
	 * @return Grid
	 */
	protected function grid() {
		$grid = new Grid(new SCoursewareInfo);

		$grid->id('Id')->sortable();
		$grid->title('标题');
		// $grid->save_name('白板状态');
		$grid->save_name('白板状态')->display(function ($s) {
			return $s ? '已上传' : '未上传';
		});
		$grid->poster('封面')->image();
		$grid->course_type('课程类型')->using(config('svs.course_type'));
		$grid->times("时长(分)");
		// $grid->admin_id('Admin id');
		$grid->created_at('创建时间');
		$grid->updated_at('更新时间');

		//禁用导出
		$grid->disableExport();
		return $grid;
	}

	/**
	 * Make a show builder.
	 *
	 * @param mixed $id
	 * @return Show
	 */
	protected function detail($id) {
		$show = new Show(SCoursewareInfo::findOrFail($id));

		$show->id('Id');
		$show->title('标题');
		$show->poster('封面');
		$show->course_type('课程类型')->as(function ($p) {
			return config("svs.course_type." . $p);
		});
		$show->times('时长(分)');
		$show->res_url('课件')->file();
		// $show->admin_id('Admin id');
		$show->created_at('创建时间');
		$show->updated_at('更新时间');

		return $show;
	}

	/**
	 * Make a form builder.
	 *
	 * @return Form
	 */
	protected function form() {
		$form = new Form(new SCoursewareInfo);

		$form->text('title', '标题')->rules('required');
		$form->image('poster', '封面')->rules('required');
		$form->select('course_type', '课程类型')->options(config('svs.course_type'))->rules('required');
		$form->file('res_url', '上传课件')->rules('required');

		$form->number('times', '时长（分）')->rules('required|min:0');
		$form->hidden('admin_id')->value(\Admin::user()->id);

		$form->saved(function (Form $form) {
			//上传成功后修改，上传白板
			$file_id = $form->model()->id;
			dispatch(new \App\Jobs\DealWhiteFile($file_id));
			logger("saved");

		});

		$form->footer(function ($footer) {

			// 去掉`查看`checkbox
			$footer->disableViewCheck();

			// 去掉`继续编辑`checkbox
			$footer->disableEditingCheck();

			// 去掉`继续创建`checkbox
			$footer->disableCreatingCheck();

		});

		return $form;
	}
}
